export class HttpRequestAbstract {
	constructor(config) {
		this.config = config || {};
		this.interceptors = {
			request: new HttpInterceptorManager(),
			response: new HttpInterceptorManager(),
		};
	}

	middleware(config) {
		return Promise.resolve();
	}

	request(config) {
		return Promise.resolve();
	}

	get(url, config) {
		return this.request({
			...config,
			url,
			method: 'GET'
		});
	}

	upload(url, config) {
		return Promise.resolve();
	}

	delete(url, data, config) {
		return this.request({
			...config,
			url,
			data,
			method: 'DELETE'
		});
	}

	head(url, data, config) {
		return this.request({
			...config,
			url,
			data,
			method: 'HEAD'
		});
	}

	post(url, data, config) {
		return this.request({
			...config,
			url,
			data,
			method: 'POST'
		});
	}

	put(url, data, config) {
		return this.request({
			...config,
			url,
			data,
			method: 'PUT'
		});
	}

	connect(url, data, config) {
		return this.request({
			...config,
			url,
			data,
			method: 'CONNECT'
		});
	}

	options(url, data, config) {
		return this.request({
			...config,
			url,
			data,
			method: 'OPTIONS'
		});
	}

	trace(url, data, config) {
		return this.request({
			...config,
			url,
			data,
			method: 'TRACE'
		});
	}

	download(url, config) {
		return Promise.resolve();
	}

	setConfig(onSend) {
		this.config = onSend(this.config);
	}
}

class HttpInterceptorManager {
	constructor() {
		this.handlers = [];
	}

	use(onFulfilled, onRejected) {
		this.handlers.push({
			onFulfilled,
			onRejected
		});
	}

	eject(id) {
		this.handlers.splice(id, 1);
	}

	forEach(handler) {
		this.handlers.forEach(handler);
	}
}

class HttpRequest extends HttpRequestAbstract {}

export default HttpRequest;